package com.spider.admin.service.cloud;

import com.spider.admin.conf.ResultMessage;
import com.spider.admin.conf.validator.Constants;
import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.store.primary.mapper.AccountMapper;
import com.spider.admin.store.primary.mapper.CloudMapper;
import com.spider.admin.store.primary.model.CloudInfo;
import com.spider.admin.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CloudServiceImpl implements CloudService {

    @Autowired
    private CloudMapper cloudMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public int saveCloudInfo(CloudInfo cloudInfo) throws SpiderException {

        cloudInfo
                .setPid(CommonUtil.getShortUniqueId())
                .setArchiveStatus(Constants.ACTIVE.getCode());
        String tenantId = cloudInfo.getTenantId();
        if (StringUtils.isEmpty(tenantId)) {
            throw new SpiderException(ResultMessage.FAILED_PARAMS_VALIDATE.getCode(), "租户ID不能为空");
        }
        long count = accountMapper.getActiveTenantCountById(tenantId);
        if (count <= 0) {
            throw new SpiderException(ResultMessage.FAILED_PARAMS_VALIDATE.getCode(), "租户未激活或不存在");
        }
        int i = cloudMapper.saveCloud(cloudInfo);
        log.info("新增服务云信息：{}", cloudInfo.getCloudName());
        return i;
    }

    @Override
    public CloudInfo queryCloudById(String pid) throws SpiderException {
        return cloudMapper.queryCloudById(pid);
    }

    @Override
    public int updateCloudById(CloudInfo cloudInfo, String pid) throws SpiderException {
        cloudInfo.setArchiveStatus(null);
        String tenantId = cloudInfo.getTenantId();
        if (StringUtils.isNotEmpty(tenantId)) {
            long count = accountMapper.getActiveTenantCountById(tenantId);
            if (count <= 0) {
                throw new SpiderException(ResultMessage.FAILED_PARAMS_VALIDATE.getCode(), "租户未激活或不存在");
            }
        }
        long count = cloudMapper.getCloudCountByNotId(cloudInfo.getPid(), cloudInfo.getCloudName(), null, null);
        if (count > 0) {
            throw new SpiderException(ResultMessage.FAILED_PARAMS_VALIDATE.getCode(), "服务云名称已被占用");
        }
        int i = cloudMapper.updateCloudById(cloudInfo, pid);
        return i;
    }

    @Override
    public int activeCloud(String pid) throws SpiderException {
        CloudInfo cloudInfo = new CloudInfo();
        cloudInfo.setArchiveStatus(Constants.ACTIVE.getCode());
        int i = cloudMapper.updateCloudById(cloudInfo, pid);
        log.info("激活服务云：{}", pid);
        return i;
    }

    @Override
    public int archiveCloud(String pid) throws SpiderException {
        CloudInfo cloudInfo = new CloudInfo();
        cloudInfo.setArchiveStatus(Constants.ARCHIVE.getCode());
        int i = cloudMapper.updateCloudById(cloudInfo, pid);
        log.info("归档服务云：{}", pid);
        return i;
    }

    @Override
    public PageResult<CloudInfo> queryCloudByTenantId(String tenantId, Integer page, Integer pageSize, String cloudName, String cloudBasePath, String archiveStatus) throws SpiderException {
        page = page == null || page.intValue() <= 0 ? 1 : page;
        pageSize = pageSize == null ? 10 : pageSize;
        long totalCount = cloudMapper.getCloudRecordNum(tenantId, cloudName, cloudBasePath, archiveStatus);
        int totalPage = (int) Math.ceil((double) totalCount / (double) pageSize);
        page = page > totalPage ? totalPage : page;
        page = page <= 0 ? 1 : page;
        PageResult<CloudInfo> result = new PageResult<>();
        result.setCurrentPage(page);
        result.setPageSize(pageSize);
        result.setTotal(totalCount);
        result.setTotalPage(totalPage);

        List<CloudInfo> records = cloudMapper.queryCloudByTenantId((page - 1) * pageSize, pageSize, tenantId, cloudName, cloudBasePath, archiveStatus);
        result.setList(records);

        return result;
    }
}

